//package com.runyu.cordova.plugin;
//
//import java.io.ByteArrayOutputStream;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileOutputStream;
//import java.io.FileReader;
//import java.io.FileWriter;
//import java.security.KeyFactory;
//import java.security.KeyPair;
//import java.security.KeyPairGenerator;
//import java.security.PublicKey;
//import java.security.SecureRandom;
//import java.security.spec.PKCS8EncodedKeySpec;
//import java.security.spec.X509EncodedKeySpec;
//import java.util.Base64;
//import java.util.Iterator;
//
//import javax.crypto.Cipher;
//import javax.crypto.spec.IvParameterSpec;
//import javax.crypto.spec.SecretKeySpec;
//
//public class Main {
//
//	public static void genRsaKey() throws Exception {
//		// 初始化密钥生成器
//		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
//		keyPairGenerator.initialize(512, new SecureRandom());
//
//		// 生成一对公钥和私钥
//		KeyPair keyPair = keyPairGenerator.generateKeyPair();
//		byte[] privateKey = keyPair.getPrivate().getEncoded();
//		byte[] publicKey = keyPair.getPublic().getEncoded();
//
//		// 将密钥写入文件
//		String privateKeyFile = "private_key.pem";
//		String publicKeyFile = "public_key.pem";
//
//		// 使用Base64对密钥进行编码，以便存储和传输
//		String privateKeyStr = Base64.getEncoder().encodeToString(privateKey);
//		String publicKeyStr = Base64.getEncoder().encodeToString(publicKey);
//
//		// 将编码后的密钥写入文件
//		try (FileOutputStream privateKeyOut = new FileOutputStream(privateKeyFile);
//				FileOutputStream publicKeyOut = new FileOutputStream(publicKeyFile)) {
//			privateKeyOut.write(privateKeyStr.getBytes());
//			publicKeyOut.write(publicKeyStr.getBytes());
//		}
//	}
//
//	private static String privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAmvxy8egEYreqLQnmSCIqKj3rDytXBlwBSNF/XPjHITR8/xEuPXCTOggYre9iQtqb1GRdn0Zn8qVT6nBxDIDhywIDAQABAkBVBYhJDG7beMXyTyv1Bkj7Q1xfxfbHllFeEQ89gXw7yiLIauqeJ/3ck+1tXtHS/MNUJMQ11WejMYjBvG5oCfyBAiEA5Y9eovBQ8cKffqLrOgUIzHnbK873OKwqjwoVg6GhG+8CIQCs1kAjGtyrHjBc96zHY20qQ1m9h6rqg7VblQK+rKpr5QIgR/wwWfnv+A/mwehGyRfhDXW807z/60aZxecMng1mHwMCIQCV8sNuMW3lX6mh3pcd3VrmYcxHrJ5SG7aKH/tjpbWtKQIgSZLGNdeOsV2EUWPUd/dCYaCnGZF79rlr3pWeRvSutGU=";
//
//	public static void encode() throws Exception {
//		String serial = "123456";
//		FileReader fileReader = new FileReader("public_key.pem");
//		char[] buffer = new char[1024];
//		int len = 0;
//		StringBuilder sb = new StringBuilder();
//		while ((len = fileReader.read(buffer)) != -1) {
//			sb.append(new String(buffer, 0, len));
//		}
//		fileReader.close();
//		byte[] decode = Base64.getDecoder().decode(sb.toString());
//		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decode);
//		PublicKey publicKey = keyFactory.generatePublic(keySpec);
//		Cipher cipher = Cipher.getInstance("RSA");
//		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//		byte[] bytes = cipher.doFinal(serial.getBytes());
//		FileOutputStream fileOutputStream = new FileOutputStream(new File("test.d"));
//		fileOutputStream.write(bytes);
//		fileOutputStream.close();
//	}
//
//	public static void decode() throws Exception {
//		FileInputStream fileInputStream = new FileInputStream(new File("test.d"));
//		byte[] buffer = new byte[1024];
//		int len = 0;
//		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//		while ((len = fileInputStream.read(buffer)) != -1) {
//			byteArrayOutputStream.write(buffer, 0, len);
//		}
//		fileInputStream.close();
//		byte[] data = byteArrayOutputStream.toByteArray();
//		Cipher cipher = Cipher.getInstance("RSA");
//		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//		// 获取私钥
//		byte[] privateKeyBytes = Base64.getDecoder().decode(privateKey);
//		PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
//		java.security.PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
//		cipher.init(Cipher.DECRYPT_MODE, privateKey);
//		byte[] bytes = cipher.doFinal(data);
//		System.out.println(new String(bytes));
//	}
//
//	private static String aceKey = "";
//
//	public static void genRandomKey() throws Exception {
//		SecureRandom secureRandom = new SecureRandom();
//		byte[] bytes = new byte[16];
//		secureRandom.nextBytes(bytes);
//		String aesKey = Base64.getEncoder().encodeToString(bytes);
//		FileOutputStream fileOutputStream = new FileOutputStream("aes.key");
//		fileOutputStream.write(aesKey.getBytes());
//		fileOutputStream.close();
//	}
//
//	public static void testEncodeAce() throws Exception {
//		String key = "0nBxros6jjRnn2AJcobJWA==";
//		byte[] keyBytes = Base64.getDecoder().decode(key);
//		SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
////		byte[] bvs = "0F102C9F71063130".getBytes();
////		System.out.print("{");
////		for (int i = 0; i < bvs.length; i++) {
////			System.out.print(bvs[i] + ",");
////		}
//		
////		System.out.print("}");
//		byte[] bvs = {48,70,49,48,50,67,57,70,55,49,48,54,51,49,51,48};
//		IvParameterSpec ivParameterSpec = new IvParameterSpec(bvs);
//		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//		cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
//		FileInputStream fileInputStream = new FileInputStream("www/data/0.d");
//		byte[] buffer = new byte[1024];
//		int len = 0;
//		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//		while ((len = fileInputStream.read(buffer)) != -1) {
//			byteArrayOutputStream.write(buffer, 0, len);
//		}
//		fileInputStream.close();
//		byte[] byteArray = byteArrayOutputStream.toByteArray();
//		System.out.println(byteArray.length);
//		byte[] bytes = cipher.doFinal(byteArray);
//		String value = new String(bytes);
//		FileWriter fileWriter = new FileWriter("www/data/1.txt");
//		fileWriter.write(value);
//		fileWriter.close();
//	}
//
//	public static String generateRandomIV() {
//		SecureRandom secureRandom = new SecureRandom();
//		byte[] ivBytes = new byte[8]; // 8字节的IV
//		secureRandom.nextBytes(ivBytes);
//		return bytesToHex(ivBytes); // 转换为十六进制字符串
//	}
//
//	private static final char[] HEX_ARRAY = "0123456789ABCDEFGHIJKLMNOPQRST".toCharArray();
//
//	public static String bytesToHex(byte[] bytes) {
//		char[] hexChars = new char[bytes.length * 2];
//		for (int j = 0; j < bytes.length; j++) {
//			int v = bytes[j] & 0xFF;
//			hexChars[j * 2] = HEX_ARRAY[v >>> 4];
//			hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
//		}
//		return new String(hexChars);
//	}
//
//	public static void main(String[] args) throws Exception {
//		testEncodeAce();
//	}
//
//};